<?php
/**
 * User: Drery
 * Date: 2015/11/27
 * Time: 16:41
 */

namespace api\behaviors;

use Yii;
use yii\base\ActionEvent;
use yii\base\Behavior;
use yii\base\Controller;

class ApiRecordor extends Behavior
{
    /**
     * @inheritdoc
     */
    public function attach($owner)
    {
        $this->owner = $owner;
        $owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeAction']);
    }

    /**
     * @inheritdoc
     */
    public function detach()
    {
        if ($this->owner) {
            $this->owner->off(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeAction']);
            $this->owner = null;
        }
    }

    public function beforeAction(ActionEvent $event)
    {
        $controller_id = $event->action->controller->id;
        $action_id = $event->action->id;
        Yii::info('['.$controller_id.'/'.$action_id.']params:'.json_encode($_REQUEST),'custom');
        return true;
    }
}